package main.java.br.com.projeto.domain.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.PersistenceException;

import main.java.br.com.projeto.domain.entity.ServicoOS;

public class DaoServicoOS extends Dao<ServicoOS>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2088413932521181881L;

	public DaoServicoOS() {
		super(ServicoOS.class);
	}
	
	public ServicoOS insert(ServicoOS entity) {
		try {
			this.beginTransaction();		
			getManager().persist(entity);
			this.commit();
			this.closeTransaction();
			return entity;
		} catch (PersistenceException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e2) {
			this.rollback();
			e2.printStackTrace();
			return null;
		}
	}
	
	public List<ServicoOS> listByOrdemServico(Long numOs) {
		String hql = "select ServicoOS from ServicoOS servicoOS "
				+ "where servicoOS.ordemServico.numOs = :numOs ";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("numOs", numOs);

		return this.findOneResult(hql, parameters);
	}
	
	public List<ServicoOS> listByFuncionarioId(Long funcionarioId) {
		String hql = "select ServicoOS from ServicoOS servicoOS "
				+ "where servicoOS.funcionarioId = :funcionarioId ";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("funcionarioId", funcionarioId);

		return this.findOneResult(hql, parameters);
	}
	
	public List<ServicoOS> listByServicoId(Long servicoId) {
		String hql = "select ServicoOS from ServicoOS servicoOS "
				+ "where servicoOS.servicoId = :servicoId ";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("servicoId", servicoId);

		return this.findOneResult(hql, parameters);
	}
	
	public List<ServicoOS> listByServicoIdAndData(Long servicoId, Date data) {
		String hql = "select ServicoOS from ServicoOS servicoOS "
				+ "where servicoOS.servicoId = :servicoId "
				+ "and servicoOS.data = :data";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("servicoId", servicoId);
		parameters.put("data", data);


		return this.findOneResult(hql, parameters);
	}
	
	public List<ServicoOS> listByClienteIdAndData(Long clienteId, Date data) {
		String hql = "select ServicoOS from ServicoOS servicoOS "
				+ "where servicoOS.cliente.id = :clienteId "
				+ "and servicoOS.data = :data";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("clienteId", clienteId);
		parameters.put("data", data);


		return this.findOneResult(hql, parameters);
	}
	
	public List<ServicoOS> listByFuncionarioIdAndData(Long funcionarioId, Date data) {
		String hql = "select ServicoOS from ServicoOS servicoOS "
				+ "where servicoOS.funcionarioId = :funcionarioId "
				+ "and servicoOS.data = :data";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("funcionarioId", funcionarioId);
		parameters.put("data", data);


		return this.findOneResult(hql, parameters);
	}
	
	public List<ServicoOS> listByData(Date data) {
		String hql = "select ServicoOS from ServicoOS servicoOS "
				+ "where servicoOS.data = :data";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("data", data);


		return this.findOneResult(hql, parameters);
	}
}
